home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / tn3270 / tcpio.c < prev    next >
C/C++ Source or Header  |  1992-04-17  |  34KB  |  1,475 lines

  1. /*
  2.  *  tn3270 for the Macintosh Source Code
  3.  *  Brown University Computing and Information Services
  4.  *  Version 2.4d7  April, 1992
  5.  *  Copyright (c) 1988, 1989, 1990, 1991, 1992 by Brown University and by
  6.  *  Peter John DiCamillo.
  7.  *
  8.  *  Permission is granted to any individual or institution to use, copy,
  9.  *  or redistribute the binary version of this software and its
  10.  *  documentation provided this notice and the copyright notices are
  11.  *  retained.  Permission is granted to any individual or non-profit
  12.  *  institution to use, copy, modify, or redistribute the source files
  13.  *  of this software provided this notice and the copyright notices are
  14.  *  retained.  This software may not be distributed for profit, either
  15.  *  in original form or in derivative works, nor can the source be
  16.  *  distributed to other than an individual or a non-profit institution.
  17.  *  Any  individual or group interested in seeing and/or using these
  18.  *  source files but who are prevented from doing so by the above
  19.  *  constraints should contact Don Wolfe, Assistant Vice-President for
  20.  *  Computer Systems at Brown University, (401) 863-7250, for possible
  21.  *  software licensing of the source developed at Brown.
  22.  *
  23.  *  Brown University and Peter John DiCamillo make no representations
  24.  *  about the suitability of this software for any purpose.
  25.  *
  26.  *  BROWN UNIVERSITY AND PETER JOHN DICAMILLO GIVE NO WARRANTY, EITHER
  27.  *  EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
  28.  *  INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
  29.  *  WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
  30.  *
  31.  */
  32.  
  33. #if !defined(USEDUMP)
  34.     #include "maclib.h"
  35.     #include "termdef.h"
  36.     #include "tn3270funcs.h"
  37.     #include "globals.h"
  38. #else
  39.     #pragma load "tn3270DumpFile"
  40. #endif
  41.  
  42. #include <AppleTalk.h>
  43.  
  44. #include "kip.h"
  45. #include "netevent.h"
  46. #include "hostform.h"
  47. #include "NCSAfuncs.h"            /* must follow hostform.h */
  48.  
  49. #pragma segment 3270tcp
  50.  
  51. #define CFIGFILE    "\pNCSA Telnet Settings"
  52. #define HTELNET    23
  53.  
  54. unsigned char myipnum[4];    /* my IP number */
  55. long kipns;                    /* name server from KIP */
  56. long mynetmask;                /* my network mask */
  57. unsigned char KIPserver[4];    /* AppleTalk address of KIP gateway */
  58. int KIP;                    /* KIP gateway is present */
  59. int HFS;                    /* HFS file system being used */
  60. extern int EtherNet;        /* "The all-powerful! */
  61. #ifdef TCPDRV
  62. static unsigned char KIPzone[33] = "\p*";    /* just to keep the linker happy */
  63. static unsigned char *KIPnameptr;
  64. #else
  65. extern unsigned char KIPzone[33];    /* zone for KIP from config file */
  66. extern unsigned char *KIPnameptr;    /* NamesTableEntry pointer for KIP */
  67. #endif
  68.  
  69. char unldATP;                /* flag to unload .ATP driver */
  70. char unldNBP;                /* flag to unload NBP */
  71. #ifdef TCPDRV
  72. char needResClose;            /* flag to close DNR resolver */
  73. #endif
  74. char dynam;                    /* myipnum was obtained dynamically */
  75. char snetmask;                /* mask was obtained from file */
  76. char kipreg;                /* flag to unregister address */
  77. NamesTableEntry myreg;        /* my registration entry */
  78. short savedvol;                /* saved volref to restore after configuration */
  79. char tcpinitok = 0;            /* tcp network stuff active */
  80. extern short launchvol;        /* default volume when we were launched */
  81.  
  82. extern int button;            /* true if mouse down at startup */
  83. extern char ftpavail;        /* flag for FTP enabled */
  84. extern short systemvol;        /* volume with system files */
  85. extern struct Point sfgpoint;    /* SFGetFile location */
  86. extern main();                /* for debugging */
  87.  
  88. #include "stdio.h"
  89.  
  90. unsigned char * rcvbuff;
  91. short rcvmax;
  92.  
  93. #ifdef TCPDRV
  94. OSErr OpenResolver(unsigned char *fileName);
  95. OSErr CloseResolver(void);
  96. #endif
  97.  
  98. OSErr tcp_init(void)
  99. {
  100. long *HFSp= (long *)1014;
  101. Handle version;
  102. short i, ioerror;
  103. #ifdef TCPDRV
  104. OSErr rc;
  105. #endif
  106.  
  107. putln("tcp_init called");
  108.                                         /* version info. for debugging */
  109. strcpy(dbgmsg, "Version string: ");
  110. version = GetResource('GFTM', 0);
  111. if (version != 0) {
  112.     i = strlen(dbgmsg);
  113.     memcpy(dbgmsg+i, (*version)+1, (*version)[0]);
  114.     dbgmsg[i+(*version)[0]] = 0;
  115.     }
  116. else strcpy(dbgmsg, "Version string not available.");
  117. putln(dbgmsg);
  118. strcpy(dbgmsg, "VM file transfer version: ");
  119. version = GetResource('WSFR', 161);
  120. if (version != 0) {
  121.     i = strlen(dbgmsg);
  122.     memcpy(dbgmsg+i, (*version)+13, (*version)[12]-1);
  123.     dbgmsg[i+(*version)[12]-1] = 0;
  124.     while (dbgmsg[i] != 0) {
  125.         dbgmsg[i] = ((unsigned char *)(*xtabh))[dbgmsg[i]];
  126.         i++;
  127.         }
  128.     }
  129. else strcpy(dbgmsg, "VM file transfer version unknown.");
  130. putln(dbgmsg);
  131. sprintf(dbgmsg, "main at %lx", main);    /* in case we're using a map */
  132. putln(dbgmsg);                            /* for debugging */
  133.  
  134. KIP = 0;            /* no KIP gateway yet */
  135. HFS = (*HFSp) > 0L;    /* check if under HFS */
  136. unldATP = 0;        /* haven't loaded ATP */
  137. unldNBP = 0;        /* haven't loaded NBP */
  138. #ifdef TCPDRV
  139. needResClose = 0;    /* haven't opened resolver */
  140. #endif
  141. kipreg = 0;            /* haven't registered our address */
  142. kipns = 0;            /* no nameserver from KIP */
  143.  
  144. /* allocate buffers */
  145. rcvbuff = (unsigned char *)NewPtr(614L);    /* 550 + 64 */
  146. if (rcvbuff == 0L) {
  147.     stgalert("tcp rcv buffer", "NewPtr", 614L);
  148.     return(-1);
  149.     }
  150. rcvmax = 512;
  151.  
  152. /* save current volume, and attempt to find config.tel */
  153. GetVol(dbgmsg, &savedvol);
  154. checkhostfile();
  155.  
  156. #ifdef TCPDRV
  157. rc = OpenResolver(0);
  158. if (rc != noErr) {
  159.     sprintf(dbgmsg, "Unable to open MacTCP: Result code %d from OpenResolver.", rc);
  160.     putln(dbgmsg);
  161.     stopterr(dbgmsg, 0);
  162.     DisposPtr(rcvbuff);
  163.     return(-2);
  164.     }
  165. needResClose = 1;
  166. #endif
  167.  
  168. /* initialize and get host information */
  169. ioerror = Snetinit();        /* calls initipnum() */
  170. sprintf(dbgmsg, "return code %d from Snetinit", ioerror);
  171. putln(dbgmsg);
  172. SetVol(0L, savedvol);    /* restore initial volume */
  173. if ((ioerror != 0) && (ioerror != -2)) {
  174.     ShowAllErrors();
  175. #ifdef TCPDRV
  176.     CloseResolver();
  177. #endif
  178.     DisposPtr(rcvbuff);
  179.     return(-3);
  180.     }
  181. tcpinitok = 1;                    /* call Stask shutdown net stuff */
  182. /* if (ioerror == -2) ioerror = reconfigNetwork(); */    /* RARP failed */
  183. if (ioerror != 0) {
  184.     ShowAllErrors();
  185.     netshut();
  186.     tcpinitok = 0;
  187. #ifdef TCPDRV
  188.     CloseResolver();
  189. #endif
  190.     DisposPtr(rcvbuff);
  191.     return(-4);
  192.     }
  193.     
  194. if (EtherNet) 
  195.     dynam = (myipnum[0] == 'R') && (myipnum[1] == 'A') &&
  196.             (myipnum[2] == 'R') && (myipnum[3] == 'P');
  197. netgetip(myipnum);                /* get net number, in case RARPed */
  198. netgetmask((unsigned char *)&mynetmask);    /* mask may have changed too */
  199. ftpavail = Sfmode();            /* get initial FTP settings */
  200. return(0);
  201. }
  202.  
  203. int initipnum(int dummy)
  204.         /* "dummy" used to be "button" */
  205. {
  206. #pragma unused(dummy)
  207.  
  208. NTElement lkpresult;
  209. BDSType retBDS;
  210. IPGP gateSend, gateRec;
  211. short count, rc;
  212. unsigned char nsaddr[4];
  213. short ioerror;
  214.  
  215. /* open AppleTalk drivers if not using Ethernet */
  216.  
  217. if (!EtherNet) {
  218.     ioerror = openatdrv();
  219.     if (ioerror != 0) quit();
  220.  
  221.     /* check for KIP gateway, and if found load number from it */
  222.     ioerror = NBld();                /* attempt to load NBP */
  223.     if (ioerror != 0) {
  224.         stoperr(loadalrt, 0);
  225.         quit();
  226.         }
  227.                                     /*  look for gateway */
  228.     rc = nblookup("\p=", "\pIPGATEWAY", KIPzone, &lkpresult, &count);
  229.     sprintf(dbgmsg, "rc = %d, count = %d looking for =:IPGATEWAY@%p",
  230.             rc, count, KIPzone);
  231.     putln(dbgmsg);
  232.     if ((rc == 0) && (count > 0)) {
  233.         KIP = 1;
  234.         memcpy(KIPserver, &lkpresult.nteAddress, 4);    /* get gateway addr. */
  235.         retBDS[0].buffSize = sizeof(gateRec);        /* get nameserver address */
  236.         retBDS[0].buffPtr = (Ptr)&gateRec;
  237.         gateSend.opcode = 3;
  238.         rc = atsend((AddrBlock *)KIPserver, (unsigned char *)&gateSend,
  239.                 sizeof(struct IPGP), (BDSPtr)retBDS);
  240.         if (rc == 0) {                    /* got an address */
  241.             kipns = gateRec.ipname;
  242.             if (kipns != 0) {
  243.                 Ssetns((unsigned char *)&kipns);
  244.                 memcpy(nsaddr, &kipns, 4);
  245.                 sprintf(dbgmsg, 
  246.                     "Using name server address from gateway: %d.%d.%d.%d",
  247.                     nsaddr[0], nsaddr[1], nsaddr[2], nsaddr[3]);
  248.                 putln(dbgmsg);
  249.                 }
  250.             else {
  251.                 sprintf(dbgmsg, 
  252.                     "Name server address not available from gateway.");
  253.                 putln(dbgmsg);
  254.                 }
  255.             }
  256.         else {
  257.             sprintf(dbgmsg, "Return code %d getting nameserver address", rc);
  258.             putln(dbgmsg);
  259.             }
  260.         }
  261.     }
  262. else KIP = 0;
  263.  
  264. ioerror = getipinfo();            /* try to get IP number and mask */
  265. if (button || (ioerror != 0)) {
  266.     ioerror = promptip(0);            /* get info from user */
  267.     if (ioerror != 0) stoperr(cfigalrt, 0);    /* report error saving config */
  268.     ioerror = getipinfo();            /* try again */
  269.     if (ioerror != 0) {             /* exit with error if still failed */
  270.         stoperr(addralrt, 0);
  271.         quit();
  272.         }
  273.     }
  274.  
  275. if (KIP) {                        /* for KIP, need to register number */
  276.     ioerror = kpregister(myipnum);    
  277.     if (ioerror != 0) {         /* exit with error if failed */
  278.         stoperr(regalrt, 0);
  279.         quit();
  280.         }
  281.     else kipreg = 1;
  282.     }
  283.  
  284. netsetip(myipnum);                /* set IP number */
  285. netsetmask((unsigned char *)&mynetmask);    /* set network mask */
  286. putln("initipnum done");
  287. return(0);
  288. }
  289.  
  290. short getipinfo(void)            /* get IP number and mask */
  291. {
  292. short error, rc;
  293. unsigned char strbuf[256];
  294. long dfltmask;
  295.  
  296. mynetmask = 0;                /* initialize mask */
  297. dynam = 0;                    /* no dynamic number yet */
  298. snetmask = 0;                /* no mask from file */
  299.                             /* get IP number */
  300. error = getstr(strbuf, 256, 256);
  301. if (error == 0) error = decodeIPnum(strbuf, myipnum);
  302. if (error) putln("no IP number from resource");
  303. else putln("got IP number from resource");
  304.  
  305. if (error) {
  306.     if (EtherNet) {                /* dynamic if Ether and no IP number */
  307.         strncpy(myipnum, "RARP", 4);
  308.         dynam = 1;
  309.         error = 0;
  310.         putln("using RARP");
  311.         }
  312.     else if (KIP) {                /* dynamic if KIP and no IP number */
  313.         error = kpdynamip((long *)myipnum);
  314.         if (!error) {
  315.             dynam = 1;
  316.             putln("got dynamic IP number");
  317.             }
  318.         }
  319.     }
  320.                             /* get subnet mask */
  321. if (!error) {
  322.     rc = getstr(strbuf, 256, 258);
  323.     if (rc == 0) {
  324.         if (sscanf(strbuf, "%lx", &mynetmask) != 1)
  325.             calcmask(&mynetmask);
  326.         else {
  327.             calcmask(&dfltmask);
  328.             snetmask = (mynetmask != dfltmask);
  329.             }
  330.         }
  331.     else calcmask(&mynetmask);
  332.     sprintf(dbgmsg, "net mask = %08lx", mynetmask);
  333.     putln(dbgmsg);
  334.     }
  335.  
  336. if (error) {
  337.     memset(myipnum, 0, 4);    /* ipnum = 0 if errors */
  338.     mynetmask = 0;            /* mask too */
  339.     putln("error from getipinfo");
  340.     }
  341. return(error);
  342. }
  343.  
  344. OSErr promptip(short ro)
  345. {
  346.  
  347. DialogPtr dlgptr;
  348. DialogPeek dStorage;
  349. WindowPtr behind;
  350. pascal Boolean (*filterProc) ();
  351. short oldHit, itemHit;
  352. short gtype;
  353. Handle iphandle, dynamhandle, maskhandle;
  354. Rect gbox;
  355. unsigned char ipstr[30], maskstr[20], change;
  356. unsigned char tempnum[20];
  357. OSErr rc;
  358. short rfile;
  359. int hi, low, node;
  360. Size ressize;
  361. Handle temph;
  362. char rarp;
  363.  
  364. #ifdef TCPDRV
  365. if (ro) showdrv();
  366. return(0);
  367. #endif
  368.  
  369. dStorage = 0;
  370. behind = (WindowPtr)-1;        /* 272 = r/w, 273 = r/o */
  371. dlgptr = GetNewDialog(272+ro, dStorage, behind);
  372. ctrwindow(dlgptr);
  373.  
  374. /* get handles to various items */
  375. GetDItem(dlgptr, 3, >ype, &iphandle, &gbox);
  376. GetDItem(dlgptr, 4, >ype, &dynamhandle, &gbox);
  377. GetDItem(dlgptr, 7, >ype, &maskhandle, &gbox);
  378.  
  379. /* get initial settings */
  380.             /* disable dynamic address if AppleTalk and no KIP */
  381. if (!EtherNet && !KIP) HiliteControl((ControlHandle)dynamhandle, 255);
  382.             /* set ip number radio buttons */
  383. if (!ro) setit(2, dlgptr, (!dynam));
  384. setit(4, dlgptr, dynam && (KIP || EtherNet));
  385.             /* set current ip number */
  386. rc = 4;
  387. if (!ro) rc = getstr(ipstr, 30, 256);
  388. if (rc != 0) {
  389.     rarp = (myipnum[0] == 'R') && (myipnum[1] == 'A') &&
  390.            (myipnum[2] == 'R') && (myipnum[3] == 'P');
  391.     if (!rarp) sprintf(ipstr, "%d.%d.%d.%d", myipnum[0], myipnum[1],
  392.                                              myipnum[2], myipnum[3]);
  393.     else strcpy(ipstr, "0.0.0.0");
  394.     }
  395. c2pstr(ipstr);
  396. SetIText(iphandle, ipstr);
  397.             /* set current ip mask */
  398. cvtmask(mynetmask, maskstr);
  399. c2pstr(maskstr);
  400. SetIText(maskhandle, maskstr);
  401.             /* set "use subnetting" check box */
  402. setit(5, dlgptr, snetmask);
  403.             /* for info. display, get AppleTalk numbers */
  404. if (ro) {
  405.     if (EtherNet) hi = low = node = 0;
  406.     else getATaddress( &hi, &low, &node);
  407.     sprintf(tempnum, "%d.%d", hi, low);
  408.     GetDItem(dlgptr, 10, >ype, &temph, &gbox);
  409.     c2pstr(tempnum);
  410.     SetIText(temph, tempnum);
  411.     sprintf(tempnum, "%d", node);
  412.     GetDItem(dlgptr, 12, >ype, &temph, &gbox);
  413.     c2pstr(tempnum);
  414.     SetIText(temph, tempnum);
  415.     GetDItem(dlgptr, 13, >ype, &temph, &gbox);
  416.     if (EtherNet) SetIText(temph, "\p[RARP]");
  417.     else if (KIP) SetIText(temph, "\p[KIP]");
  418.     }
  419. else {
  420.     GetDItem(dlgptr, 9, >ype, &temph, &gbox);
  421.     if (EtherNet) SetIText(temph, "\p[RARP]");
  422.     else if (KIP) SetIText(temph, "\p[KIP]");
  423.     }
  424.  
  425. /* ready to display the dialog */
  426. ShowWindow(dlgptr);
  427. arrowcursor();
  428. /* frame the default selection */
  429. framedflt(dlgptr);
  430.  
  431. filterProc = DlgFilter;
  432. oldHit = 0;
  433. change = 0;
  434. while(1) {
  435.     ModalDialog(filterProc, &itemHit);
  436.     if (itemHit != 1) change = 1;
  437.     if ((oldHit > 0) && (oldHit != itemHit))
  438.     switch(oldHit) {
  439.         case 3:                /* IP number editText */
  440.             GetIText(iphandle, ipstr);
  441.             p2cstr(ipstr);
  442.             rc = decodeIPnum(ipstr, tempnum);
  443.             if (rc) SetIText(iphandle, "\p0.0.0.0");
  444.             break;
  445.         case 7:                /* mask editText */
  446.             GetIText(maskhandle, maskstr);
  447.             p2cstr(maskstr);
  448.             if (sscanf(maskstr, "%lx", &mynetmask) != 1)
  449.                 calcmask(&mynetmask);
  450.             cvtmask(mynetmask, maskstr);
  451.             c2pstr(maskstr);
  452.             SetIText(maskhandle, maskstr);
  453.             break;
  454.         default:
  455.             break;
  456.         }
  457.     oldHit = itemHit;
  458.     switch(itemHit) {
  459.         case 2:                    /* IP number buttons */
  460.         case 4:
  461.             dynam = (itemHit == 4);
  462.             setit(2, dlgptr, (!dynam));
  463.             setit(4, dlgptr, dynam);
  464.             break;
  465.         case 5:                    /* use subnetting box */
  466.             snetmask ^= 1;
  467.             setit(5, dlgptr, snetmask);
  468.             break;
  469.         }
  470.     if (itemHit == 1) break;
  471.     }
  472.  
  473. /* just return if only "OK" was hit or r/o */
  474. if ((!change) || ro) {
  475.     DisposDialog(dlgptr);
  476.     return(0);
  477.     }
  478.  
  479. /* set ipstr and maskstr to the text to save */
  480.         /* use number unless dynamic specified */
  481. if (dynam) ipstr[0] = 0;
  482. else {
  483.     GetIText(iphandle, ipstr);
  484.     p2cstr(ipstr);
  485.     }
  486.         /* use mask if subnetting requested */
  487. if (snetmask) {
  488.     GetIText(maskhandle, maskstr);
  489.     p2cstr(maskstr);
  490.     }
  491. else maskstr[0] = 0;
  492.  
  493. /* done with dialog now */
  494. DisposDialog(dlgptr);
  495.  
  496. /* delete any existing number and mask resources */
  497. sysvol(0);
  498. rfile = OpenResFile(CFIGFILE);
  499. if (rfile != -1) {
  500.     temph = GetResource('STR ', 256);
  501.     if (temph != 0) if (HomeResFile(temph) == rfile) {
  502.         RmveResource(temph);
  503.         }
  504.     temph = GetResource('STR ', 258);
  505.     if (temph != 0) if (HomeResFile(temph) == rfile) {
  506.         RmveResource(temph);
  507.         }
  508.     CloseResFile(rfile);
  509.     }
  510. else {
  511.     Create(CFIGFILE, 0, 'NCSA', 'IPNO');
  512.     CreateResFile(CFIGFILE);
  513.     }
  514.  
  515. /* return if not storing number and mask */
  516. if ((ipstr[0] == 0) && (maskstr[0] == 0)) {
  517.     sysvol(1);
  518.     return(0);
  519.     }
  520.  
  521. /* get handles to strings to write out */
  522. if (ipstr[0] != 0) {
  523.     c2pstr(ipstr);
  524.     ressize = ipstr[0] + 1;
  525.     iphandle = NewHandle(ressize);
  526.     if (iphandle == 0) {
  527.         sysvol(1);
  528.         return(1);
  529.         }
  530.     memcpy((*iphandle), ipstr, ipstr[0]+1);
  531.     }
  532. else iphandle = 0;
  533.  
  534. if (maskstr[0] != 0) {
  535.     c2pstr(maskstr);
  536.     ressize = maskstr[0] + 1;
  537.     maskhandle = NewHandle(ressize);
  538.     if (maskhandle == 0) {
  539.         if (iphandle != 0) DisposHandle(iphandle);
  540.         sysvol(1);
  541.         return(2);
  542.         }
  543.     memcpy((*maskhandle), maskstr, maskstr[0]+1);
  544.     }
  545. else maskhandle = 0;
  546.  
  547.  
  548. /* write the required resources */
  549. rfile = OpenResFile(CFIGFILE);
  550. if ((ResError() == 0) && (iphandle != 0)) 
  551.     AddResource(iphandle, 'STR ', 256, "\pIP Number");
  552. if ((ResError() == 0) && (maskhandle != 0))
  553.     AddResource(maskhandle, 'STR ', 258, "\pNetwork Mask");
  554. rc = ResError();
  555. if (rfile != -1) CloseResFile(rfile);
  556. if (rc == 0) rc = ResError();
  557. sysvol(1);
  558. return(rc);
  559. }
  560.  
  561. void updhostrsc(unsigned char *s)        /* store new default host name */
  562. {
  563. short rfile;
  564. Handle temph;
  565. Size ressize;
  566.  
  567. /* delete any existing host resource */
  568. sysvol(0);
  569. rfile = OpenResFile(CFIGFILE);
  570. if (rfile != -1) {
  571.     temph = GetResource('STR ', 257);
  572.     if (temph != 0) if (HomeResFile(temph) == rfile) {
  573.         RmveResource(temph);
  574.         }
  575.     CloseResFile(rfile);
  576.     }
  577. else {
  578.     Create(CFIGFILE, 0, 'NCSA', 'IPNO');
  579.     CreateResFile(CFIGFILE);
  580.     }
  581.  
  582. /* return if not storing host name */
  583. if (s[0] == 0) {
  584.     sysvol(1);
  585.     return;
  586.     }
  587.  
  588. /* get handles to string to write out */
  589. c2pstr(s);
  590. ressize = s[0] + 1;
  591. temph = NewHandle(ressize);
  592. if (temph == 0) {
  593.     p2cstr(s);
  594.     sysvol(1);
  595.     return;
  596.     }
  597. memcpy((*temph), s, s[0]+1);
  598. p2cstr(s);
  599.  
  600. /* write the required resource */
  601. rfile = OpenResFile(CFIGFILE);
  602. if ((ResError() == 0) && (temph != 0)) {
  603.     AddResource(temph, 'STR ', 257, "\pDefault Host");
  604.     }
  605. if (rfile != -1) {
  606.     CloseResFile(rfile);
  607.     }
  608. sysvol(1);
  609. return;
  610. }
  611.  
  612. void showdrv(void)
  613. {
  614. DialogPtr dlgptr;
  615. DialogPeek dStorage;
  616. WindowPtr behind;
  617. pascal Boolean (*filterProc) ();
  618. short itemHit;
  619. short gtype;
  620. Handle iphandle;
  621. Rect gbox;
  622. unsigned char ipstr[30];
  623.  
  624. dStorage = 0;
  625. behind = (WindowPtr)-1;    
  626. dlgptr = GetNewDialog(280, dStorage, behind);
  627. ctrwindow(dlgptr);
  628.  
  629. GetDItem(dlgptr, 4, >ype, &iphandle, &gbox);
  630. sprintf(ipstr, "%d.%d.%d.%d", myipnum[0], myipnum[1],
  631.                              myipnum[2], myipnum[3]);
  632. c2pstr(ipstr);
  633. SetIText(iphandle, ipstr);
  634.  
  635. /* ready to display the dialog */
  636. ShowWindow(dlgptr);
  637. arrowcursor();
  638. /* frame the default selection */
  639. framedflt(dlgptr);
  640.  
  641. /* show dialog */
  642. filterProc = DlgFilter;
  643. ModalDialog(filterProc, &itemHit);
  644. DisposDialog(dlgptr);
  645. }
  646.  
  647. void calcmask(long *mask)        /* calculate mask from IP number */
  648. {
  649. if (!(myipnum[0] & 0x80))
  650.         (*mask) = 0xff000000;            /* Class A */
  651.     else if ((myipnum[0] & 0xc0) == 0x80)
  652.         (*mask) = 0xffff0000;            /* Class B */
  653.     else
  654.         (*mask) = 0xffffff00;            /* Class C */
  655. }
  656.  
  657. void tcpdflthost(unsigned char *s)
  658. {
  659. short rc;
  660.  
  661. rc = getstr(s, 256, 257);
  662. if (rc != 0) strcpy(s, "?");    /* If "?", prompt will be issued */
  663. }
  664.  
  665. long tcpgetrsrv(cnr *cp)
  666. {
  667.  
  668. #ifdef TCPDRV
  669.  
  670. /* if (init) return(131072); */
  671. if (cp->online) return(20480);
  672. else return(40960);
  673.  
  674. #else
  675.  
  676. #pragma unused(cp)
  677.  
  678. /* if (init) return(135168); */
  679. return(26624);
  680.  
  681. #endif
  682. }
  683.  
  684. void tcplgin(cnr *cp)
  685. {
  686.                         /* initialize variables for new conn. */
  687. login_init(cp);
  688.                         /* attempt to get IP address for host */
  689. cp->mp = Sgethost(cp->hostonly);
  690. ShowAllErrors();
  691. if (!(cp->mp)) {        /* table lookup failed; try name server */
  692.     cp->myport = Sdomain(cp->hostonly);
  693.     sprintf(dbgmsg, "return code %d from Sdomain for %s",
  694.         cp->myport, cp->hostonly);
  695.     putln(dbgmsg);
  696.     if (cp->myport == -1) {
  697.         stoperr(domalrt, cp);
  698.         if (cp->apiopen) {
  699.             apiopenerr(openResFailed, cp);
  700.             }
  701.         removeconnection(cp);
  702.         }
  703.     else cp->connstate = 2;
  704.     }
  705. else {                /* open a connection from table lookup */
  706.     cp->myport = Snetopen(cp->mp, rdportnum(cp));
  707.     if (cp->myport < 0) {
  708.         stoperr(topnalrt, cp);
  709.         if (cp->apiopen) {
  710.             apiopenerr(openConnFailed, cp);
  711.             }
  712.         removeconnection(cp);
  713.         }
  714.     else cp->connstate = 3;
  715.     }
  716. }
  717.  
  718. void tcplgout(char keep, cnr *cp)
  719. {
  720. if (cp->connstate > 0) netclose(cp->myport);
  721. sessionlgout(keep, cp);
  722. }
  723.  
  724. void tcp_end(void)
  725. {
  726. putln("tcp_end called");
  727.  
  728. if (kipreg) {
  729.     kpunregister();        /* un-register our address */
  730.     kipreg = 0;
  731.     }
  732. if (tcpinitok) {
  733.     netshut();            /* undo Snetinit */
  734.     tcpinitok = 0;
  735.     }
  736. NBuld();            /* unload NBP */
  737. ATuld();            /* unload ATP */
  738. if (rcvbuff != 0) {
  739.     DisposPtr(rcvbuff);
  740.     rcvbuff = 0;
  741.     }
  742. #ifdef TCPDRV
  743. if (needResClose) {
  744.     CloseResolver();
  745.     }
  746. needResClose = 0;
  747. #endif
  748. }
  749.  
  750. /* tcpwrite - call netwrite to get all data written */
  751. void tcpwrite(unsigned char *dptr, short dsize, cnr *cp)
  752. {
  753. short cnt;
  754. static unsigned long * Ticks = (unsigned long *)0x16a;
  755. unsigned long limit;
  756. writeinfo *infoptr;
  757. short infosize;
  758.  
  759. if (writedebug) {
  760.     infoptr = new_writeinfo(cp);
  761.     infoptr->start_ticks = *Ticks;
  762.     infoptr->length = dsize;
  763.     infosize = dsize;
  764.     if (infosize > INFODATASIZE) infosize = INFODATASIZE;
  765.     memcpy(infoptr->data, dptr, infosize);
  766.     }
  767. if (dsize > 1) {        /* skip for single characters */
  768.     cp->online = 0;        /* for efficiency in line mode */
  769.     newstat(cp);
  770.     }
  771. limit = (*Ticks) + 60 * cp->cs.timeout * cp->cs.retries;
  772. while (dsize > 0) {
  773.     if (cp->cs.dblevel > 2) dbgdump("netwrite", dptr, dsize);
  774.     netpush(cp->myport);
  775.     cnt = netwrite(cp->myport, dptr, dsize);
  776.     if (writedebug) {
  777.         infoptr->calls++;
  778.         }
  779.     if (cp->cs.dblevel > 2) {
  780.         sprintf(dbgmsg, "netwrite wrote %d bytes", cnt);
  781.         putln(dbgmsg);
  782.         }
  783.     if (cnt > 0) {
  784.         dptr += cnt;
  785.         dsize -= cnt;
  786.         limit = (*Ticks) + 60 * cp->cs.timeout * cp->cs.retries;
  787.         }
  788.     myStask();
  789.     if ((*Ticks) > limit) {
  790.         if (writedebug) {
  791.             infoptr->end_ticks = *Ticks;
  792.             infoptr->result = 1;
  793.             }
  794.         cp->online = 0;
  795.         cp->kblcode = 3;
  796.         cp->ioerror = 4;
  797.         newstat(cp);
  798.         closeresponse(closeNetFail, cp);
  799.         tcplgout(1, cp);
  800.         stoperr(wrtalrt, cp);
  801.         return;
  802.         }
  803.     }
  804. if (writedebug) {
  805.     infoptr->end_ticks = *Ticks;
  806.     infoptr->result = 0;
  807.     }
  808. if (cp->online == 0) {
  809.     cp->online = 1;
  810.     newstat(cp);
  811.     }
  812. }
  813.  
  814. void tcpin(unsigned char asc, unsigned char chr,
  815.            unsigned char shift, cnr *cp)
  816. {
  817.  
  818. if (!(cp->logon)) {                /* Return or CLEAR for new session, else beep */
  819.     if (cp->connstate == 0) kbnew(chr, cp);
  820.     else if ((cp->connstate == 4) && (!(cp->kblock)))
  821.                 tcpkbin(asc, chr, shift, cp);
  822.     else beep(cp);
  823.     return;
  824.     }
  825.  
  826. if (chr == 196) {            /* perform program reset immediately */
  827.     tcp_reset(cp);
  828.     xfrst(cp);
  829.     invldscr(cp);
  830.     return;
  831.     }
  832.  
  833. if (chr == 185) {            /* perform keyboard immediately */
  834.     hndkbd(chr, shift, cp);
  835.     return;
  836.     }
  837.  
  838. if (chr == 197) {            /* perform print screen immediately */
  839.     prscreen(cp);
  840.     return;
  841.     }
  842.  
  843. if ((cp->kbqsize == KBQMAX) || (cp->vmxbgn && (!(cp->vmxsub)))) {
  844.     beep(cp);            /* error if xfer mode or queue full */
  845.     kerr(3, cp);
  846.     }
  847. else {                        /* else add to keyboard queue */
  848.     (cp->kbqueue)[cp->kbqsize].code = chr;
  849.     (cp->kbqueue)[cp->kbqsize].shift = shift;
  850.     cp->kbqsize++;
  851.     }
  852. }
  853.  
  854. void tcp_reset(cnr *cp)
  855. {
  856. if (cp->logon) {
  857.     cp->kblock = cp->kblcode = 0;
  858.     cp->online = 1;
  859.     }
  860. cp->lastwcc = 0x02;
  861. cp->kb_err = 0;
  862. cp->kbqsize = 0;
  863. cp->aplmode = cp->insmode = 0;
  864. cp->fixbracket = cp->cs.std_brack && (!(cp->aplmode)) &&
  865.     (cp->stdfont != ALAFONT);
  866. newstat(cp);
  867. }
  868.  
  869. void ATuld(void)        /* if necessary, unload .ATP driver */
  870. {
  871. CntrlParam cp;
  872.  
  873. if (sw_active() || (!unldATP)) return;
  874.  
  875. unldATP = 0;
  876. cp.ioCRefNum = -11;        /* .ATP reference number */
  877. PBClose((ParmBlkPtr)&cp, 0);
  878. }
  879.  
  880. OSErr NBld(void)        /* load NBP */
  881. {
  882. CntrlParam cp;
  883. OSErr rc;
  884.  
  885. if (unldNBP) return(0);
  886. cp.ioCRefNum = -10;            /* .MPP driver number */
  887. cp.csCode = 249;
  888. rc = PBControl((ParmBlkPtr)&cp, 0);
  889. if (rc == 0) unldNBP = 1;
  890. return(rc);
  891. }
  892.  
  893. void NBuld(void)            /* unload NBP */
  894. {
  895. CntrlParam cp;
  896.  
  897. if (!unldNBP) return;
  898. cp.ioCRefNum = -10;            /* .MPP driver number */
  899. cp.csCode = 255;
  900. PBControl((ParmBlkPtr)&cp, 0);
  901. unldNBP = 0;
  902. }
  903.  
  904. OSErr nblookup(unsigned char *object, unsigned char *type,
  905.                unsigned char *zone, NTElement *result, short *count)
  906. {
  907. MPPParamBlock Mpb;
  908. OSErr rc;
  909. unsigned char entity[99];
  910.  
  911. NBPSetEntity(entity, object, type, zone);
  912. Mpb.NBPinterval = 3;
  913. Mpb.NBPcount = 4;
  914. Mpb.NBPentityPtr = entity;
  915. Mpb.NBPretBuffPtr = (Ptr)result;
  916. Mpb.NBPretBuffSize = sizeof(NTElement);
  917. Mpb.NBPmaxToGet = 1;
  918. Mpb.MPPioRefNum = -10;            /* .MPP driver number */
  919. Mpb.MPPcsCode = 251;            /* lookup name */
  920. rc = PBControl((ParmBlkPtr)&Mpb, 0);
  921. (*count) = Mpb.NBPnumGotten;
  922. return(rc);
  923. }
  924.  
  925. OSErr atsend(AddrBlock *ablock, unsigned char *sendaddr,
  926.              short sendlen, BDSPtr retbds)
  927. {
  928. ATPParamBlock Apb;
  929. OSErr rc;
  930.  
  931. Apb.ATPuserData = 0;
  932. Apb.ATPcsCode = 255;
  933. Apb.ATPioRefNum = -11;
  934. Apb.ATPatpFlags = 0;
  935. Apb.ATPaddrBlock = (*ablock);
  936. Apb.ATPreqLength = sendlen;
  937. Apb.ATPreqPointer = sendaddr; 
  938. Apb.ATPbdsPointer = (Ptr)retbds;
  939. Apb.ATPnumOfBuffs = 1;
  940. Apb.ATPtimeOutVal = 1;            /* try every second */
  941. Apb.ATPretryCount = 30;            /* up to 30 times */
  942. rc = PBControl((ParmBlkPtr)&Apb, 0);
  943. return(rc);
  944. }
  945.  
  946. OSErr kpdynamip(long *ipnum)
  947. {
  948. IPGP gateRec, gateSend;
  949. BDSType retBDS;
  950. OSErr rc;
  951.  
  952. putln("obtaining dynamic IP number");
  953.  
  954. retBDS[0].buffSize = sizeof(gateRec);
  955. retBDS[0].buffPtr = (Ptr)&gateRec;
  956. gateSend.opcode = 1;
  957. rc = atsend((AddrBlock *)KIPserver, (unsigned char *)&gateSend,
  958.         sizeof(struct IPGP), (BDSPtr)retBDS);
  959. if (rc != 0) return(rc);
  960. (*ipnum) = gateRec.ipaddress;
  961. return(0);
  962. }
  963.  
  964. OSErr kpregister(unsigned char *ipnum)
  965. {
  966. MPPParamBlock Mpb;
  967. OSErr rc;
  968. unsigned char temps[20];
  969.  
  970. sprintf(temps, "%d.%d.%d.%d", ipnum[0], ipnum[1], ipnum[2], ipnum[3]);
  971. sprintf(dbgmsg, "registering %s", temps);
  972. putln(dbgmsg);
  973. c2pstr(temps);
  974. if (KIPnameptr) kpunregister();
  975. KIPnameptr = (unsigned char *)&myreg;
  976. memset(&myreg, 0, sizeof(NamesTableEntry));
  977. NBPSetNTE((Ptr)&myreg, temps, "\pIPADDRESS", KIPzone, 72);
  978. Mpb.MPPioRefNum = -10;
  979. Mpb.MPPcsCode = 253;
  980. Mpb.NBPinterval = 5;
  981. Mpb.NBPcount = 5;
  982. Mpb.NBPntQElPtr = (Ptr)&myreg; 
  983. Mpb.NBPverifyFlag = 1; 
  984. rc = PBControl((ParmBlkPtr)&Mpb, 0);
  985. sprintf(dbgmsg, "rc = %d from NBPRegister", rc);
  986. putln(dbgmsg);
  987. return(rc);
  988. }
  989.  
  990. void kpunregister(void)
  991. {
  992. MPPParamBlock Mpb;
  993. OSErr rc;
  994.  
  995. Mpb.MPPioRefNum = -10;
  996. Mpb.MPPcsCode = 252;
  997. Mpb.NBPentityPtr = (Ptr)myreg.nt.entityData; 
  998. rc = PBControl((ParmBlkPtr)&Mpb, 0);
  999. KIPnameptr = 0;
  1000. sprintf(dbgmsg, "rc = %d from NBPRemove", rc);
  1001. putln(dbgmsg);
  1002. }
  1003.  
  1004. OSErr openatdrv(void)        /* open AppleTalk drivers */
  1005. {
  1006. static unsigned char *SPConfig = (unsigned char *)0x1fb;
  1007. static unsigned char *PortBUse = (unsigned char *)0x291;
  1008. IOParam openblk;
  1009. OSErr rc;
  1010.  
  1011. if ((*PortBUse & 0x80) == 0x80)     {    /* check if port B is in use    */
  1012.                                     /* port B not in use:            */
  1013.     if ((*SPConfig & 0x0f) > 1) {    /* error unless unconfigured    */
  1014.         stoperr(portalrt, 0);        /* or configured for AppleTalk    */
  1015.         return(-98);                
  1016.         }
  1017.     else {                            /* else open MPP driver            */
  1018.         openblk.ioPermssn = 0;
  1019.         openblk.ioNamePtr = (StringPtr)"\p.MPP";
  1020.         rc = PBOpen((ParmBlkPtr)&openblk, 0);
  1021.         if (rc != 0) {
  1022.             stoperr(loadalrt, 0);
  1023.             return(rc);
  1024.             }
  1025.         }
  1026.     }
  1027. else {                                /* port B in use:            */
  1028.     if ((*PortBUse & 0x0f) != 1) {    /* error if not in use by     */
  1029.         stoperr(portalrt, 0);        /* AppleTalk                */
  1030.         return(-97);
  1031.         }
  1032.     }
  1033.  
  1034.                                 /* load ATP if not already loaded */
  1035. if ((*PortBUse & 0x10) == 0) {
  1036.     openblk.ioPermssn = 0;
  1037.     openblk.ioNamePtr = (StringPtr)"\p.ATP";
  1038.     rc = PBOpen((ParmBlkPtr)&openblk, 0);
  1039.     if (rc != 0) {
  1040.         stoperr(loadalrt, 0);
  1041.         return(rc);
  1042.         }
  1043.     unldATP = 1;                /* remember we loaded .ATP */
  1044.     }
  1045.  
  1046. return(0);
  1047. }
  1048.  
  1049. int isHFS(void)
  1050. {
  1051. return(HFS);
  1052. }
  1053.  
  1054. cvtmask(long mask, unsigned char *s)
  1055. {
  1056. register short i, len;
  1057.  
  1058. sprintf(s, "%08lx", mask);
  1059. len = strlen(s);
  1060. for (i=0; i < len; i++)
  1061.     s[i] = toupper(s[i]);
  1062. }
  1063.  
  1064. Boolean decodeIPnum(unsigned char *s, unsigned char *myipnum)
  1065. {
  1066.     short i;
  1067.     int node, hi, low;
  1068.     unsigned char temp[50];
  1069.     char addrflag;
  1070.  
  1071.     for (i=0; i<4; i++) myipnum[i]=0;
  1072.     i=0;
  1073.     addrflag = 0;
  1074.  
  1075.     while((*s) && (i<4) ) {
  1076.         switch( *s) {
  1077.             case '0':
  1078.             case '1':
  1079.             case '2':
  1080.             case '3':
  1081.             case '4':
  1082.             case '5':
  1083.             case '6':
  1084.             case '7':
  1085.             case '8':
  1086.             case '9':
  1087.                 myipnum[i]= myipnum[i] *10 + (*s-'0');
  1088.                 break;
  1089.             case '.':
  1090.                 i++;
  1091.                 break;
  1092.             case 'H':
  1093.             case 'h':
  1094.                 if (addrflag == 0) {
  1095.                     getATaddress( &hi, &low, &node);
  1096.                     addrflag = 1;
  1097.                     }
  1098.                 myipnum[i]=hi;
  1099.                 break;
  1100.             case 'L':
  1101.             case 'l':
  1102.                 if (addrflag == 0) {
  1103.                     getATaddress( &hi, &low, &node);
  1104.                     addrflag = 1;
  1105.                     }
  1106.                 myipnum[i]=low;
  1107.                 break;
  1108.             case 'N':
  1109.             case 'n':
  1110.                 if (addrflag == 0) {
  1111.                     getATaddress( &hi, &low, &node);
  1112.                     addrflag = 1;
  1113.                     }
  1114.                 myipnum[i]=node;
  1115.                 break;
  1116.             default:
  1117.                 break;
  1118.             }
  1119.         s++;
  1120.         }
  1121.     
  1122.     sprintf( temp," IPNUM= %d.%d.%d.%d", (int) myipnum[0], (int) myipnum[1], (int) myipnum[2],
  1123.                                          (int) myipnum[3]);
  1124.     putln(temp);
  1125.     
  1126.     if (i<3) return( 1);
  1127.         else return( 0);
  1128. }
  1129.  
  1130. void ShowAllErrors(void)
  1131. {
  1132. int ev, what, dat;
  1133.  
  1134. ev = Sgetevent( ERRCLASS,&what,&dat);
  1135. while (ev) {
  1136.     NetError(dat);
  1137.     ev = Sgetevent( ERRCLASS,&what,&dat);
  1138.     }
  1139. }
  1140.  
  1141. void NetError(int code)
  1142. {
  1143. unsigned char buffer1[100], buffer2[100];
  1144. int  ev, what;
  1145. short class;
  1146. pascal Boolean (*filterProc) ();
  1147. static char nullstr[1] = {0};
  1148.  
  1149. buffer1[0]=0;
  1150. buffer2[0]=0;
  1151.  
  1152. if (code<0) {
  1153.     strncpy( buffer2, neterrstring(code), 99);
  1154.     ev = Sgetevent( ERRCLASS,&what,&code);
  1155.     if (ev)
  1156.         strncpy( buffer1, neterrstring(code), 99);
  1157.     }
  1158. else
  1159.     strncpy( buffer1, neterrstring(code), 99);
  1160.  
  1161. class = code/100;
  1162. if ((code == 402) || (code == 405)) class = 5;
  1163. if ((class==1 || class==5 || class==9)  && (code!=503)  ) {
  1164.     /* Ok, I'll show the user these */
  1165.     c2pstr(buffer1);
  1166.     c2pstr(buffer2);
  1167.     ParamText( buffer1, buffer2, nullstr, nullstr);
  1168.     p2cstr(buffer1);
  1169.     p2cstr(buffer2);
  1170.     filterProc = DlgFilter;
  1171.     arrowcursor();
  1172.     if (class == 9) StopAlert(274, filterProc);
  1173.     else if (class == 5) CautionAlert(275, filterProc);
  1174.     else NoteAlert(276, filterProc);
  1175.     ParamText(nullstr, nullstr, nullstr, nullstr);
  1176.     }
  1177. if (buffer1[0])
  1178.     putln(buffer1);
  1179. if (buffer2[0])
  1180.     putln(buffer2);
  1181. }
  1182.  
  1183. void OtherError(unsigned char *mess1, unsigned char *mess2)
  1184. {
  1185. unsigned char buffer1[100], buffer2[100];
  1186. pascal Boolean (*filterProc) ();
  1187. static char nullstr[1] = {0};
  1188.  
  1189. buffer1[0]=0;
  1190. buffer2[0]=0;
  1191.  
  1192. strncpy(buffer1, mess1, 99);
  1193. strncpy(buffer2, mess2, 99);
  1194.  
  1195. c2pstr(buffer1);
  1196. c2pstr(buffer2);
  1197.  
  1198. ParamText( buffer1, buffer2, nullstr, nullstr);
  1199. filterProc = DlgFilter;
  1200. StopAlert(274, filterProc);
  1201. ParamText( nullstr, nullstr, nullstr, nullstr);
  1202. }
  1203.  
  1204. void tcpinfo(cnr *cp)
  1205. {
  1206. DialogPtr dlgptr;
  1207. DialogPeek dStorage;
  1208. WindowPtr behind;
  1209. short gtype, itemHit;
  1210. Rect gbox;
  1211. pascal Boolean (*filterProc) ();
  1212. Handle hosth, iph, stath;
  1213. unsigned char ipstr[20];
  1214.  
  1215. dStorage = 0;
  1216. behind = (WindowPtr)-1;
  1217. dlgptr = GetNewDialog(277, dStorage, behind);
  1218. ctrwindow(dlgptr);
  1219.  
  1220. /* get handles we will need */
  1221. GetDItem(dlgptr, 3, >ype, &hosth, &gbox);
  1222. GetDItem(dlgptr, 5, >ype, &iph, &gbox);
  1223. GetDItem(dlgptr, 7, >ype, &stath, &gbox);
  1224.  
  1225. /* set host name */
  1226. if (cp->connstate < 2) {
  1227.     c2pstr(cp->hostonly);
  1228.     SetIText(hosth, cp->hostonly);
  1229.     p2cstr(cp->hostonly);
  1230.     }
  1231. else if ((cp->mp)->hname == 0) {
  1232.         if ((cp->mp)->sname == 0) SetIText(hosth, "\punknown");
  1233.         else if (strcmp((cp->mp)->sname, "default") == 0)
  1234.                                 SetIText(hosth, "\punknown");
  1235.              else {
  1236.                 c2pstr((cp->mp)->sname);
  1237.                  SetIText(hosth, (cp->mp)->sname);
  1238.                 p2cstr((cp->mp)->sname);
  1239.                 }
  1240.         }
  1241.      else {
  1242.         c2pstr((cp->mp)->hname);
  1243.          SetIText(hosth, (cp->mp)->hname);
  1244.         p2cstr((cp->mp)->hname);
  1245.         }
  1246.  
  1247. /* set ip number */
  1248. if (cp->connstate < 3) SetIText(iph, "\punknown");
  1249. else {
  1250.     sprintf(ipstr, "%d.%d.%d.%d", (cp->mp)->hostip[0],
  1251.     (cp->mp)->hostip[1], (cp->mp)->hostip[2], (cp->mp)->hostip[3]);
  1252.     c2pstr(ipstr);
  1253.     SetIText(iph, ipstr);
  1254.     }
  1255.  
  1256. /* set status */
  1257. switch (cp->connstate) {
  1258.     case 0:                        /* waiting for name server */
  1259.         SetIText(stath, "\pIdle - press Return to open a connection");
  1260.         break;
  1261.     case 2:                        /* waiting for name server */
  1262.         SetIText(stath, "\pwaiting for response from name server");
  1263.         break;
  1264.     case 3:                        /* waiting for connection */
  1265.         SetIText(stath, "\pwaiting for connection to complete");
  1266.         break;
  1267.     case 4:                        /* negotiating settings */
  1268.         if (cp->hadascii) SetIText(stath, "\pconnected in line mode");
  1269.         else SetIText(stath, "\pconnected: negotiating settings");
  1270.         break;
  1271.     case 5:                        /* if 0, must be logged on */
  1272.         SetIText(stath, "\p3270 session established");
  1273.         break;
  1274.     default:                    /* shouldn't happen */
  1275.         SetIText(stath, "\pI'm confused -- status unknown");
  1276.         break;
  1277.     }
  1278.  
  1279. /* ready to display the dialog */
  1280. ShowWindow(dlgptr);
  1281. arrowcursor();
  1282. /* frame the default selection */
  1283. framedflt(dlgptr);
  1284.  
  1285. /* display the dialog until OK selected */
  1286. filterProc = DlgFilter;
  1287. itemHit = 0;
  1288. while (itemHit != 1) ModalDialog(filterProc, &itemHit);
  1289.  
  1290. DisposDialog(dlgptr);
  1291. }
  1292.  
  1293. int tcp_session(cnr *cp)        /* return TRUE if session active */
  1294. {
  1295. return(cp->logon || (cp->hadascii && (cp->connstate == 4)));
  1296. }
  1297.  
  1298. short rdportnum(cnr *cp)
  1299.                     /* return port number from connhostname, or default */
  1300. {
  1301. register short i, count, len, port;
  1302. register unsigned char *s;
  1303.  
  1304. len = strlen(cp->connhostname);
  1305. if (len < 3) return(HTELNET);     /* need at least two colons and digit */
  1306.  
  1307. count = 0;
  1308. for (i=0; i < len; i++) {
  1309.     if ((cp->connhostname)[i] == ':') count++;
  1310.     if (count == 2) break;
  1311.     }
  1312. if (count < 2) return(HTELNET);
  1313. s = cp->connhostname + i + 1;        /* s = host number string */
  1314. if (s[0] == '\0') return(HTELNET);
  1315. if ((strcmp(s, "IRC") == 0) || (strcmp(s, "irc") == 0)) {
  1316.     cp->serverconn = 1;
  1317.     cp->servermode = 0;
  1318.     cp->serverflags = 1;
  1319.     port = 6667;
  1320.     }
  1321. else if ((strcmp(s, "SMTP") == 0) || (strcmp(s, "smtp") == 0)) {
  1322.     cp->serverconn = 1;
  1323.     cp->servermode = 0;
  1324.     cp->serverflags = 0;
  1325.     port = 25;
  1326.     }
  1327. else if ((strncmp(s, "SERVER=", 7) == 0) || (strncmp(s, "server=", 7) == 0)) {
  1328.     cp->serverconn = 1;
  1329.     cp->servermode = 0;
  1330.     cp->serverflags = 0;
  1331.     port = atoi(s+7);
  1332.     }
  1333. else {
  1334.     cp->serverconn = cp->servermode = cp->serverflags = 0;
  1335.     port = atoi(s);
  1336.     }
  1337. if (port == 0) {
  1338.     sprintf(dbgmsg, "Port specification %s is invalid", s);
  1339.     putln(dbgmsg);
  1340.     stoperr(pnumalrt, cp);
  1341.     port = HTELNET;
  1342.     }
  1343. if (cp->serverconn) {
  1344.     sprintf(dbgmsg, "Using non-Telnet connection mode");
  1345.     putln(dbgmsg);
  1346.     }
  1347. sprintf(dbgmsg, "port number = %d", port);
  1348. putln(dbgmsg);
  1349. return(port);
  1350. }
  1351.  
  1352. OSErr getstr(unsigned char *s, short lim, short num)
  1353. {
  1354. short rfile, rc;
  1355. Handle temph;
  1356. unsigned char * tempp;
  1357.  
  1358. sysvol(0);
  1359. rfile = OpenResFile(CFIGFILE);
  1360. if (rfile != -1) {
  1361.     temph = GetResource('STR ', num);
  1362.     if (temph != 0) if (HomeResFile(temph) == rfile) {
  1363.         tempp = *temph;
  1364.         if (tempp[0] > lim-1) rc = 12;
  1365.         else {
  1366.             memcpy(s, tempp, tempp[0]+1);
  1367.             p2cstr(s);
  1368.             sprintf(dbgmsg, "STR %d = \"%s\"", num, s);
  1369.             putln(dbgmsg);
  1370.             rc = 0;
  1371.             }
  1372.         }
  1373.     else rc = 8;
  1374.     CloseResFile(rfile);
  1375.     }
  1376. else rc = 4;
  1377. sysvol(1);
  1378. sprintf(dbgmsg, "getstr rc = %d for STR %d", rc, num);
  1379. putln(dbgmsg);
  1380. return(rc);
  1381. }
  1382.  
  1383. void sysvol(short reset)
  1384. {
  1385. unsigned char strbuff[256];
  1386. static short dfltvol;
  1387.  
  1388. if (reset) SetVol(0L, dfltvol);
  1389. else {
  1390.     GetVol(strbuff, &dfltvol);
  1391.     SetVol(0L, systemvol);
  1392.     }
  1393. }
  1394.  
  1395. void tcpquit(OSErr rc)
  1396. {
  1397. /* error "rc" from opening MacTCP */
  1398. sprintf(dbgmsg, "Unable to open MacTCP.  Result code = %d.", rc);
  1399. stopterr(dbgmsg, 0);
  1400. quit();
  1401. }
  1402.  
  1403. void quit(void)
  1404. {
  1405. ioend();
  1406. macend();
  1407. ExitToShell();
  1408. }
  1409.  
  1410. void DisplayMacBinary(void)
  1411. {
  1412. }
  1413.  
  1414. void checkhostfile(void)
  1415. {
  1416. OSErr rc;
  1417. short fnum;
  1418. Point where;
  1419. ProcPtr fileFilter;
  1420. DlgHookProcPtr dlgHook;
  1421. short numTypes;
  1422. SFTypeList typeList;
  1423. SFReply reply;
  1424. unsigned char hostfile[12];
  1425.  
  1426. strcpy(hostfile, "config.tel");
  1427. rc = fsrdopen(hostfile, savedvol, &fnum);
  1428. if (rc == 0) {
  1429.     FSClose(fnum);
  1430.     return;
  1431.     }
  1432. /* couldn't find the file on the default volume, so try the launch volume */
  1433. SetVol(0L, launchvol);
  1434. rc = fsrdopen(hostfile, launchvol, &fnum);
  1435. if (rc == 0) {
  1436.     FSClose(fnum);
  1437.     return;
  1438.     }
  1439. /* now try the system volume */
  1440. SetVol(0L, systemvol);
  1441. rc = fsrdopen(hostfile, systemvol, &fnum);
  1442. if (rc == 0) {
  1443.     FSClose(fnum);
  1444.     return;
  1445.     }
  1446. /* finally, allow the user to locate it */
  1447. SetVol(0L, savedvol);
  1448. note_err(hostfalrt, 0);            /* tell users what to do with SFGetFile */
  1449. where = sfgpoint;
  1450. fileFilter = 0;
  1451. numTypes = 1;
  1452. typeList[0] = 'TEXT';
  1453. dlgHook = 0;
  1454. arrowcursor();
  1455. SFGetFile(where, "\pSelect the file \"config.tel\":",
  1456.     (FileFilterProcPtr)fileFilter, numTypes, typeList, dlgHook, &reply);
  1457. if (reply.good == 0) return;
  1458. SetVol(0L, reply.vRefNum);
  1459. }
  1460.  
  1461. /* dummy routine to allow linking with NCSA/BYU code */
  1462.  
  1463. void userftpd(int code, int myport)
  1464. {
  1465. #pragma unused(code, myport)
  1466. }
  1467.  
  1468. /* routine use in NCSA to display FTP messages */
  1469. void ftpmess(unsigned char *s)
  1470. {
  1471. putln("FTP message:");
  1472. putln(s);
  1473. }
  1474.  
  1475.